package producer.broker;

import org.omg.PortableServer.THREAD_POLICY_ID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Executable;
import java.util.concurrent.*;

/**
 * @Desc: "异步发送线程池"
 * @Author: caixiang
 * @DATE: 2020/6/23 11:56
 */
public class AsyncBaseQueue {
    static Logger logger = LoggerFactory.getLogger(AsyncBaseQueue.class);


    //java 虚拟机 可用的线程数
    private static final int THREAD_SIZE = Runtime.getRuntime().availableProcessors();

    private static final int QUEUE_SIZE = 10000;

    private static ExecutorService senderAsync =
            new ThreadPoolExecutor(THREAD_SIZE, THREAD_SIZE, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(QUEUE_SIZE)
                    , new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    Thread t = new Thread(r);
                    t.setName("rabbitmq_client_async_sender");
                    return null;
                }
            }, new RejectedExecutionHandler() {
                //拒绝策略
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    logger.error("async sender is error rejected,runnable :{} ,executor : {} ",r,executor);
                }
            }
            );
    public static void submit(Runnable runnable){
        senderAsync.submit(runnable);
    }
}
